// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Collections.Generic;

namespace svs.Mobile.BeyondMedia.FeedCore
{
	public sealed class TrackComparer<T> : IComparer<T> where T : Track
	{
		#region Member variables
		private readonly CoreHelper.SortOrder m_sortOrder;
		#endregion

		#region constructor

		public TrackComparer(CoreHelper.SortOrder sortingOrder)
		{
			m_sortOrder = sortingOrder;
		}

		#endregion

		#region Public Methods

		public int Compare(T x, T y)
		{

			if (x == y)
				return 0;

			switch(m_sortOrder)
			{
				case CoreHelper.SortOrder.ByDateAsc:
					if (x.LastModifiedDate == null && y.LastModifiedDate == null)
						return 0;

					if (x.LastModifiedDate.HasValue)
						return x.LastModifiedDate.Value.CompareTo(y.LastModifiedDate);
					break;

				case CoreHelper.SortOrder.ByDateDesc:
					if (x.LastModifiedDate == null && y.LastModifiedDate == null)
						return 0;

					if (y.LastModifiedDate.HasValue)
						return y.LastModifiedDate.Value.CompareTo(x.LastModifiedDate);
					break;

				case CoreHelper.SortOrder.ByNameAsc:

					if (x.Name == null && y.Name == null)
						return 0;

					if (x.Name != null)
						return x.Name.CompareTo(y.Name);
					break;

				case CoreHelper.SortOrder.ByNameDesc:

					if (x.Name == null && y.Name == null)
						return 0;

					if (y.Name != null)
						return y.Name.CompareTo(x.Name);
					break;
			}

			return -1;
		}

		#endregion
	}
}